<?php
class Product_Model_Product
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_Product();
	}
	
	public static function getInstance(){
		return new Product_Model_Product();
	}
	
	public function getAdapter(){
		return $this->_table->getAdapter(); 
	}
	/**
	*Create A Record  
	*@param rowSet $row Product's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Product's data 
	*@param int $product_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$row['product_last_modified'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $product_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $id); 
		return $this->_table->delete($where);
	}
	
	public function getByProductId($id){	
		$select = $this->_table->select()->where('product_id = ?', $id);
		if($rowset = $this->_table->fetchRow($select)){
			return $rowset->toArray();
		}	
		return false;			
	}
    public function getByCondition($condition=array(),$type='*',$orderby="product_id",$pageSize=0,$page=1){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_title"]) && $condition["product_title"]!=''){
            $select->where(" product_title like ?","%".$condition["product_title"]."%");
        }
		if(isset($condition["product"]) && $condition["product"]!=''){
            $select->where(" product_title like '%".$condition["product"]."%' or product_id='".$condition["product"]."'");
        }
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" customer_id = ?",$condition["customer_id"]);
        }		
		if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
			if(preg_match("/-/", $condition["product_sku"])){
           		 $select->where(" CONCAT(customer_id,'-',product_sku) = ?",$condition["product_sku"]);				
			}else{
           		 $select->where(" CONCAT(customer_id,product_sku) = ?",$condition["product_sku"]);
				
			}
        }
        
        if(isset($condition["product_ref_sku"]) && $condition["product_ref_sku"]!=''){
        	$select->where(" product_ref_sku = ?",$condition["product_ref_sku"]);
        }
        
        if(isset($condition["product_status"]) && $condition["product_status"]!=''){
        	$select->where(" product_status = ?",$condition["product_status"]);
        }
        

        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
            $select->order($orderby);
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	/**
	* 
	* @param int $productId
	* @param int $customerId
	* @return zend_db_table_row
	*/
	public function getProduct($productId){
		$where = $this->_table->getAdapter()->quoteInto("product_id = ?",$productId);
		return $this->_table->fetchRow($where);
	}
	
	/**
	* 
	* @param int $productId
	* @param int status
	* @return zend_db_table_row
	*/
	public function changeProductStatus($productId,$status){
		$arr = array("product_status"=>$status);
		$arr['product_last_modified'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto('product_id = ?', $productId);
		return $this->_table->update($arr, $where);	
	}
	
	/**
	 * 获得产品包材重量
	 * @param array $customerId
	 * @return float
	 */
	public function getProductPackageWeightByProductId($productIds){
		$productIds = implode(",",$productIds);
		$sql = "select MAX(pp_weight)  from product_package a,product_package_map b,product c where c.product_id in( $productIds) and b.package_id = a.pp_id and c.product_id = b.product_id";
		//		echo $sql;
		return $this->_table->getAdapter()->fetchOne($sql);
	}
	/**
	* 检查sku是否存在
	* @param string $sku
	* @param int $customerId
	* @return boolean
	*/
	public function getByCustomerIdAndSku($customerId,$sku){
		$select = $this->_table->select()->where("customer_id = ?",$customerId)->where("product_sku = ?",$sku);
		return  $this->_table->fetchRow($select);
	}
	
	public function getRowByCond($condition=array(),$type='*'){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
		if(isset($condition["product_id"]) && $condition["product_id"]!=''){
            $select->where(" product_id = ?",$condition["product_id"]);
        }
		if(isset($condition["customer_id"]) && $condition["customer_id"]!=''){
            $select->where(" customer_id = ?",$condition["customer_id"]);
        }
		if(isset($condition["product_sku"]) && $condition["product_sku"]!=''){
            $select->where(" product_sku = ?",$condition["product_sku"]);
        }
        if(isset($condition["onepiece_sku"]) && $condition["onepiece_sku"]!=''){
			if(preg_match("/-/", $condition["onepiece_sku"])){
           		 $select->where(" CONCAT(customer_id,'-',product_sku) = ?",$condition["onepiece_sku"]);				
			}else{
           		 $select->where(" CONCAT(customer_id,product_sku) = ?",$condition["onepiece_sku"]);
				
			}
        }
		$sql=$select->__toString();
		return $this->_table->getAdapter()->fetchRow($sql);
	}
	
	
}